<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sortNo" :min="1"></el-input-number>
			</el-form-item>
			<el-form-item label="模版名称" prop="templateName">
				<el-input v-model="form.templateName" placeholder="模版名称" clearable maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="公文类型" prop="flowId">
					<el-select v-model="form.flowId" clearable @change="loadBpmFlowStep" style="width: 100%;">
						<el-option v-for="item in flowOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
			</el-form-item>
			<el-form-item label="执行步骤" prop="stepId" v-if="form.flowId!=''">
				<el-select v-model="form.stepId" clearable>
					<el-option v-for="item in stepOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="是否有效">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="模版文件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :limit="1" :data="{module:'doc'}"
								:showFileList="false" @success="addNewFile"
								tip="只能上传单个文件不要超过10M,请上传Word格式文件"
								accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<el-divider content-position="center">表单与模版文本标签对应关系</el-divider>
			<sc-form-table v-model="dataBinding" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
				<el-table-column prop="val" label="表单字段" min-width="180">
					<template #default="scope">
						<el-input v-model="scope.row.formData" placeholder="表单字段"></el-input>
					</template>
				</el-table-column>
				<el-table-column prop="val" label="模版文本标签名" min-width="180">
					<template #default="scope">
						<el-input v-model="scope.row.tags" placeholder="模版文本标签名"></el-input>
					</template>
				</el-table-column>
			</sc-form-table>
			<el-form-item label="备注" prop="remark" style="margin-top:10px">
				<el-input v-model="form.remark" maxlength="150" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
	export default {
		components: {
			AttachMenu
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				addTemplate: {
					formData: '',
					tags: ''
				},
				apiObj:this.$API.file.attach.uploadFile,
				visible: false,
				isSaveing: false,
				flowOptions:[],
				stepOptions:[],
				fileList:[],
				dataBinding:[],
				//表单数据
				form: {
					templateId:'',
					sortNo: 1,
					templateName:"",
					flowId:"",
					stepId:"",
					dataBinding:'',
					attachId:"",
					status: 1,
					model:'1',
					remark: ""
				},
				//验证规则
				rules: {
					flowId: [
						{required: true, message: '请选择公文'}
					],
					stepId: [
						{required: true, message: '请选择公文步骤'}
					],
					templateName: [
						{required: true, message: '请输入模版名称'}
					],
					attachId:[
						{required: true, message: '请上传模版文件'}
					]
				}
			}
		},
		mounted() {
			this.getBpmFlowListForSelect();
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.form.dataBinding = JSON.stringify(this.dataBinding);
				if(this.mode=="add")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.bpm.bpmTemplate.insert.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('success')
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}else if(this.mode=="edit")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.bpm.bpmTemplate.update.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.visible = false;
								this.$emit('success')
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}
			},
			async getBpmFlowListForSelect()
			{
				var res = await this.$API.bpm.bpmFlow.getBpmFlowListForSelect.get({model:'1'});
				if(res.code == 200){
					this.flowOptions = res.data;
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			async loadBpmFlowStep()
			{
				this.form.stepId='';
				if(this.form.flowId!='')
				{
					var res = await this.$API.bpm.bpmStep.getBpmFlowStepByFlowIdForSelect.get({flowId:this.form.flowId});
					if(res.code == 200){
						this.stepOptions = res.data;
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			},
			async loadBpmFlowStep2(flowId)
			{
				if(this.form.flowId!='')
				{
					var res = await this.$API.bpm.bpmStep.getBpmFlowStepByFlowIdForSelect.get({flowId:flowId});
					if(res.code == 200){
						this.stepOptions = res.data;
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			},
			addNewFile(res, file)
			{
				this.form.attachId = res.data.attachId;
			},
			setData(data){
				this.form.templateId = data.templateId
				this.form.templateName = data.templateName
				this.form.sortNo = data.sortNo
				this.form.flowId = data.flowId
				this.form.attachId = data.attachId
				this.form.status = data.status
				this.form.model = data.model
				this.form.remark = data.remark
				this.loadBpmFlowStep2(data.flowId);
				this.form.stepId = data.stepId
				if(data.dataBinding!=undefined&&data.dataBinding!='')
				{
					this.dataBinding = JSON.parse(data.dataBinding)
				}
			},
		}
	}
</script>

<style>
</style>
